package org.small.shop.phone.api.utils.format;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.small.shop.phone.api.utils.string.ObjectEmptyUtils;

public class FormatUtils {

	private static Logger logger = LoggerFactory.getLogger(FormatUtils.class);

	public static String getFormatDate(Long time) {

		return new SimpleDateFormat("yyyy-MM-dd").format(time);
	}

	public static String getFormatTime(Long time) {

		return new SimpleDateFormat("HH:mm:ss").format(time);
	}

	public static String getFormatDateTime(Long time) {

		return new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(time);
	}

	public static String getFormatDateTime(Long time, String format) {

		return new SimpleDateFormat(format).format(time);
	}

	public static Date getDate(String time, String format) {
		if (ObjectEmptyUtils.isNotEmpty(time) && ObjectEmptyUtils.isNotEmpty(format)) {
			try {
				return new SimpleDateFormat(format).parse(time);
			} catch (ParseException e) {
				logger.info(e.getMessage());
				return new Date();
			}
		} else {
			return new Date();
		}
	}

	public static String replaceFarmat(String date, String fromFarmat, String toFarmat) {

		SimpleDateFormat format = new SimpleDateFormat(fromFarmat);
		try {

			Date date2 = format.parse(date);
			return new SimpleDateFormat(toFarmat).format(date2);
		} catch (ParseException e) {
			logger.info(e.getMessage());
		}
		return null;
	}

	public static String getFormatDouble(Double number) {

		return new DecimalFormat("######0.00").format(number);
	}

	public static BigDecimal getFormatBigDecimal(BigDecimal big, String type) {

		if (big == null)
			return null;

		return !StringUtils.isEmpty(type) ? new BigDecimal(new DecimalFormat(type).format(big))
				: getFormatBigDecimal(big);
	}

	private static BigDecimal getFormatBigDecimal(BigDecimal big) {

		return big != null ? new BigDecimal(new DecimalFormat("#.00").format(big)) : null;
	}

}
